<?php

namespace app\adminapi\service;

use app\adminapi\model\log\SysLoginLogDO;

class SysLoginLogService
{
    public function addSysLoginLog(array $param)
    {
        (new SysLoginLogDO())->save($param);
    }

    public function list(array $param)
    {
        $where = [];
        if (isset($param['loginLocation'])) {
            $where[] = ['login_location', 'like', '%' . $param['loginLocation'] . '%'];
        }
        if (isset($param['userName'])) {
            $where[] = ['user_name', 'like', '%' . $param['userName'] . '%'];
        }
        if (isset($param['status'])) {
            $where[] = ['status', '=', $param['status']];
        }
        if (isset($param['timeArr'])) {
            $where[] = ['login_time', 'between', $param['timeArr']];
        }
        $paginate['list_rows'] = $param['pageSize'];
        if (isset($param['pageNo'])) {
            $paginate['page'] = $param['pageNo'];
        }
        $result = (new SysLoginLogDO())
            ->where($where)
            ->paginate($paginate)
            ->toArray();
        return [
            'total' => $result['total'],
            'list' => $result['data']
        ];
    }

    public function deleteLoginLog(array $logIds)
    {
        return SysLoginLogDO::destroy($logIds);
    }

    public function clearLoginLog()
    {
        return SysLoginLogDO::where('log_id', '>', 0)->delete();
    }
}